package org.jeecg.modules.fund.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * 基金详情爬虫
 */
@Slf4j
public class FundDetailHtmlUtil {
    /**
     * 获取基金详情
     * @param code
     * @return {"fx":"中高风险","lx":"指数型-股票 | 中高风险","gmrq":"2025-03-31","gmje":"73.16","gm":"73.16亿元（2025-03-31）","jjjl":"何如等","clr":"2005-08-29","glr":"嘉实基金","jjpj":"","gzbd":"沪深300指数 | 年化跟踪误差","j1y":"1.98%","j1n":"9.60%","j3y":"-2.12%","j3n":"2.73%","j6y":"1.04%","cll":"373.93%"}
     */
    public static JSONObject getDetail(String code){
        Document document;
        JSONObject result = new JSONObject();
        try {
            // 指数：160706、其他：450009
            document = Jsoup.connect("https://fund.eastmoney.com/" + code + ".html").get();
            /************************* 获取基本信息 *****************************************/
            Elements infoOfFund = document.select("div.infoOfFund").select("table");
            // 获取table里的每行
            Elements infoRows = infoOfFund.select("tr").select("td");
            // log.info(infoRows.html());
            // 遍历
            for (Element row : infoRows) {
                // 分隔处理
                String[] textArr = (row.text()).split("：");
                // 先去除空格，再获取首字母
                String key = PinyinUtil.getFirstLetter(textArr[0].replaceAll("\\s",""),"");
                // 特殊处理
                if("lx".equals(key)) {
                    // 提取风险等级
                    String[] lxArr;
                    if(textArr[1].contains("|")){
                        lxArr = textArr[1].split("\\|");
                    } else {
                        lxArr = textArr[1].split("-");
                    }
                    // String[] lxArr = textArr[1].split("\\|");
                    result.set("fx",lxArr[1].trim());
                } else if ("gm".equals(key)) {
                    // 提取日期
                    result.set("gmrq",ReUtil.get("（(.*?)）",textArr[1],1));
                    // 提取金额
                    int jeIndex = textArr[1].indexOf("亿元");
                    result.set("gmje",textArr[1].substring(0,jeIndex));
                }
                result.set(key,textArr.length > 1 ? textArr[1] : "");
            }
            /************************* 获取 近一年、近一个月 *****************************************/
            Elements dataItem01 = document.select("dl.dataItem01");
            // 获取收益信息
            Elements dataItem01DD = dataItem01.select("dd");
            for (int i = 0;i<dataItem01DD.size();i++) {
                Element item = dataItem01DD.get(i);
                if(i == 0){
                    //
                } else {
                    String str = item.text();
                    String[] strArr = str.split("：");
                    String key = PinyinUtil.getFirstLetter(strArr[0].replaceAll("\\s",""),"");
                    result.set(key,strArr[1]);
                }
            }
            /************************* 获取 近3年、近3个月 *****************************************/
            Elements dataItem02 = document.select("dl.dataItem02");
            // 获取收益信息
            Elements dataItem02DD = dataItem02.select("dd");

            for (int i = 0;i<dataItem02DD.size();i++) {
                Element item = dataItem02DD.get(i);
                if(i == 0){
                    //
                } else {
                    String str = item.text();
                    String[] strArr = str.split("：");
                    String key = PinyinUtil.getFirstLetter(strArr[0].replaceAll("\\s",""),"");
                    result.set(key,strArr[1]);
                }
            }

            /************************* 获取 近6月、成立以来 *****************************************/
            Elements dataItem03 = document.select("dl.dataItem03");
            // 获取收益信息
            Elements dataItem03DD = dataItem03.select("dd");
            for (int i = 0;i<dataItem03DD.size();i++) {
                Element item = dataItem03DD.get(i);
                if(i == 0){
                    //
                } else {
                    String str = item.text();
                    String[] strArr = str.split("：");
                    String key = PinyinUtil.getFirstLetter(strArr[0].replaceAll("\\s",""),"");
                    result.set(key,strArr[1]);
                }
            }
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args){
        System.out.println(getDetail("001484"));
    }
}
